﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using StudentManage.Data;
using System.Data.Linq.SqlClient;
using System.Text;

namespace StudentManage.Web.Admin.Management.PostManage
{
    public partial class PostEdit : SecurityPage
    {
        public override string PrivCode()
        {
            string code = "";
            switch ((EnumData.ProjectType)int.Parse(Request.QueryString["TypeId"]))
            {
                case EnumData.ProjectType.公益活动:
                    code = "a3608d1e0a2904e7";
                    break;
                case EnumData.ProjectType.勤工助学:
                    code = "29a95e0b4cf4c87a";
                    break;
                case EnumData.ProjectType.社会实践:
                    code = "c214424eafe08c40";
                    break;
                case EnumData.ProjectType.志愿活动:
                    code = "66b230cee9fb42b2";
                    break;
            }
            return code;
        }

        private void DropBanding()
        {
            List<TypeInfo> list = TypeInfo.Where("ProjectTypeid=@0 and IsDel=@1", Convert.ToInt32(EnumData.ProjectType.职位性质),false).ToList();
            this.Drop_Position.DataSource = list;
            this.Drop_Position.DataValueField = "id";
            this.Drop_Position.DataTextField = "TypeName";
            this.Drop_Position.DataBind();

            List<TypeInfo> listProjectType = TypeInfo.Where("ProjectTypeid=@0 and IsDel=@1", Convert.ToInt32(Request.QueryString["TypeId"]),false).ToList();
            this.Drop_ProjectType.DataSource = listProjectType;
            this.Drop_ProjectType.DataValueField = "id";
            this.Drop_ProjectType.DataTextField = "TypeName";
            this.Drop_ProjectType.DataBind();
        }

        private void LoadData()
        {
            string id = Request.QueryString["id"].ToString();
            Project md = Project.Where("id=@0", int.Parse(id)).First();
            List<xy_bmxbm> list_bm = xy_bmxbm.All().ToList();
            xy_bmxbm bm = list_bm.Where(p=>p.BMDM==md.Departmentid).FirstOrDefault();
            if (bm != null)
            {
                this.SelectDept.DeptID = bm.BMDM;
                this.SelectDept.DeptName = bm.BMMC;
            }

            if (md.RecruitRange != "")
            {
                StringBuilder sb = new StringBuilder();
                foreach (string strBMDM in md.RecruitRange.Split('|'))
                {
                    xy_bmxbm bmitem = list_bm.Where(p => p.BMDM == strBMDM).FirstOrDefault();
                    if (bmitem != null)
                    {
                        sb.Append(bmitem.BMMC + "|");
                    }
                }
                this.Select_RecruitRange.DeptName = sb.ToString().Trim('|');
                this.Select_RecruitRange.DeptID = md.RecruitRange;
            }

            this.Drop_Position.SelectedValue = md.PositionTypeId.ToString();
            this.txt_AddTime.Text = DateTime.Now.ToString("yyyy-MM-dd");
            this.txt_CreateUserid.Text = UserList.Where("id=@0", md.CreateUserid).First().UName;
            this.txt_CreateUserid2.Text = this.txt_CreateUserid.Text;
            this.txt_FuZheRenName.Text = md.FuZheRenName;
            this.txt_RecruitCount.Text = md.RecruitCount.ToString();
            this.txt_Tel.Text = md.Tel;
            this.txt_Title.Text = md.Title;
            this.FCKeditor1.Value = md.Contents;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;

            switch ((EnumData.ProjectType)int.Parse(Request.QueryString["TypeId"]))
            {
                case EnumData.ProjectType.公益活动:
                    this.lab_Name.Text = "项目活动：";
                    break;
                case EnumData.ProjectType.勤工助学:
                    this.lab_Name.Text = "职位名称：";
                    break;
                case EnumData.ProjectType.社会实践:
                    this.lab_Name.Text = "项目活动：";
                    break;
                case EnumData.ProjectType.志愿活动:
                    this.lab_Name.Text = "项目活动：";
                    break;
            }

            this.DropBanding();
            if (Request.QueryString["id"] != null)
            {
                this.LoadData();
            }
            else
            {
                this.txt_AddTime.Text = DateTime.Now.ToString("yyyy-MM-dd");
                this.txt_CreateUserid.Text = UserList.Where("UserName=@0", User.Identity.Name).First().UName;
                this.txt_CreateUserid2.Text = this.txt_CreateUserid.Text;
            }
        }

        protected void Btn_Save_Click(object sender, EventArgs e)
        {
            if (this.SelectDept.DeptID == "")
            {
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('请选择所属部门！');</script>");
                return;
            }

            if (this.Select_RecruitRange.DeptID == "")
            {
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('请选择招募范围！');</script>");
                return;
            }


            UserList Umd = UserList.Where("UserName=@0", User.Identity.Name).First();

            Project md;
            if (Request.QueryString["id"] == null)
                md = new Project();
            else
                md = Project.Where("id=@0", int.Parse(Request.QueryString["id"].ToString())).First();

            md.ProjectTypeid = Convert.ToInt32(Request.QueryString["TypeId"]);

            string xymc = "";
            if (Convert.ToBoolean(Umd.IsStudent))
            {
                xy_xsxx xs=xy_xsxx.Where("XH=@0", Umd.XH).First();
                xymc = xs.XY;
                md.DeptID = "";//学生没有隶属部门
            }
            else
            {
                xy_jgxx jg=xy_jgxx.Where("ZGH=@0", Umd.XH).First();
                xymc = jg.XYMC;
                md.DeptID = jg.BMDM;
            }

            xydm xymd = xydm.Where("XYMC=@0",xymc).FirstOrDefault();
            if(xymd==null)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('您没有隶属学院，无法操作，请及时联系管理员！');</script>");
                return;
            }
            md.College = xymd.XYDM;
            md.Departmentid = this.SelectDept.DeptID;
            md.RecruitRange = this.Select_RecruitRange.DeptID;
            md.PositionTypeId = Convert.ToInt32(this.Drop_Position.SelectedValue);
            md.AddTime = DateTime.Now;
            md.Typeid = Convert.ToInt32(this.Drop_ProjectType.SelectedValue);
            md.CreateUserid = Umd.id;
            md.FuZheRenName = this.txt_FuZheRenName.Text.Trim();
            md.RecruitCount = Convert.ToInt32(this.txt_RecruitCount.Text);
            md.Tel = this.txt_Tel.Text;
            md.Title = this.txt_Title.Text;
            md.Contents = this.FCKeditor1.Value;
            md.IsDel = false;
            md.IsAudit = false;
            if (Request.QueryString["id"] == null)
            {
                Project.Insert(md);
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('添加成功！');window.location.href='PostUnAuditList.aspx?TypeId=" + Request.QueryString["TypeId"].ToString()+"'</script>");
            }
            else
            {
                Project.Update(md, "id=@0", int.Parse(Request.QueryString["id"].ToString()));
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('修改成功！');window.location.href='PostUnAuditList.aspx?TypeId=" + Request.QueryString["TypeId"].ToString() + "'</script>");
            }
        }

        protected void Btn_Return_Click(object sender, EventArgs e)
        {
            Response.Redirect("PostUnAuditList.aspx?TypeId=" + Request.QueryString["TypeId"].ToString());
        }
    }
}